package DataBase;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DataInput extends JInternalFrame {
	Connection conn;
	
	private JLabel lblNewLabel;
	private JLabel lblEa;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JLabel label;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					DataInput frame = new DataInput();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public DataInput(Connection conn) {
		super("입력", true, true, true, false);
		this.conn = conn;
		setBounds(0, 0, 275, 199);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblEa());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLabel());

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Code");
			lblNewLabel.setFont(new Font("Verdana", Font.BOLD, 20));
			lblNewLabel.setBounds(29, 10, 63, 34);
		}
		return lblNewLabel;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("Ea");
			lblEa.setFont(new Font("Verdana", Font.BOLD, 20));
			lblEa.setBounds(29, 54, 63, 34);
		}
		return lblEa;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(104, 20, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(104, 61, 116, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저  장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					if(!code.getText().trim().equals("") && !ea.getText().trim().equals("")){
						try{
							String c = code.getText();
							int e = Integer.parseInt(ea.getText());
							
							String sql = "insert into ptbl(serial, code, ea) values(ptbl_serial.nextval, ?,?)";
							
							PreparedStatement pstmt =
									conn.prepareStatement(sql);
							pstmt.setString(1, c);
							pstmt.setInt(2, e);
							
							pstmt.executeUpdate();
							
							label.setText("'"+code.getText() + "'가 저장되었습니다.");
							code.setText("");
							ea.setText("");
							code.requestFocus();
							
						}catch (Exception ex){
							ex.printStackTrace();
							JOptionPane.showMessageDialog(null, "수량은 숫자만 입력하셔야 해요.");
							ea.requestFocus();
						}
					}else{
						if(code.getText().trim().equals("") && ea.getText().trim().equals("")){
							JOptionPane.showMessageDialog(null, "Code와 수량을 입력해주세요.");
							code.requestFocus();
						}else if(code.getText().trim().equals("")){
							JOptionPane.showMessageDialog(null, "Code를 입력해주세요.");
							code.requestFocus();
						}else if(ea.getText().trim().equals("")){
							JOptionPane.showMessageDialog(null, "수량을 입력해주세요.");
							ea.requestFocus();
						}
						
					}
					
				}
			});
			btnNewButton.setFont(new Font("나눔바른고딕", Font.BOLD, 15));
			btnNewButton.setBounds(104, 102, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("저장할 데이터를 입력하세요.");
			label.setBounds(12, 145, 235, 15);
		}
		return label;
	}
}
